<?php


class Product_mdl extends CI_Model
{
	
	const TB_PRODUCTS = 'cj_products';
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function get_product_list($page = true,$limit = 0, $offset = 0, $order = 'DESC',$ptype = 0)
	{
		if($page){
			$this->db->limit(intval($limit));
			$this->db->offset(intval($offset));
			$ptype ? $this->db->where('ptype',$ptype) : '';
			return $this->db->get(self::TB_PRODUCTS)->result_array();
		}
		else {
			$reward = array();
			$ptype ? $this->db->where('ptype',$ptype) : '';
			$reward = $this->db->get(self::TB_PRODUCTS)->result_array();
			return $reward;
		}

	}
	
	
	public function add_product($data)
	{

		$this->db->insert(self::TB_PRODUCTS,$data);
				
	}
	
	/**
	 * 
	 * 取得product数 ...
	 * @return int num
	 */
	
	public function get_product_count($ptype = 0)
	{
		$count = 0;
		$ptype ? $this->db->where('ptype',$ptype) : '';
		$count =  $this->db->count_all_results(self::TB_PRODUCTS);

		return $count;
	}
	
	/**
	 * 根据ID取得单条信息
	 */
	public function get_product_by_id($id)
	{
		$data = array();
		
		$this->db->select('*')->from(self::TB_PRODUCTS)->where('id', $id)->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
		}
		$query->free_result();
		
		return $data;
	}
	
	/**
	 *修改奖品
	 */
	public function update_product($id,$data)
	{
		$this->db->where('id', $id);
		
		return $this->db->update(self::TB_PRODUCTS, $data); 	
	}
	
	/**
	 * 删除奖品
	 * @access publci
	 * @param int $id
	 * @return bool - true,false
	 */
	
	public function del_product($id)
	{
		$this->db->delete(self::TB_PRODUCTS, array('id' => $id)); 
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
}